# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from get_proportion import get_proportion
from get_weight_by_entropy import get_weight_by_entropy
from get_weight_by_msd import get_weight_by_msd
from load_dataframe import load_dataframe
from normalizing import normalizing
import pandas as pd
import numpy as np
from plot_line import plot_line


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
# 城市：昆明市，曲靖市，玉溪市，楚雄州，红河州
city = '滇中'
# 指标类型：新型城镇化，生态环境
zb_type = '生态环境'
excel_name = zb_type + '-' + city
csh_file = 'data/年鉴统计数据/地级市数据/最终成果/' + excel_name + '.xlsx'
prename = city + '-' + zb_type + '-'
zb_file = 'data/年鉴统计数据/地级市数据/地级市指标体系.xlsx'
df_w = pd.DataFrame()  # 所有权重集合
df_sumscore = pd.DataFrame()  # 所有综合得分
# 加载指标
df_zb = pd.read_excel(zb_file, sheet_name=zb_type)
cxzb_names = np.array(df_zb.columns)
# 读取所有抽象分类层指标数据
for cx_name in cxzb_names:
    # 加载数据
    dataframe = load_dataframe(csh_file, cx_name)

    df_weight = pd.DataFrame()

    # 归一化处理
    normal_df = normalizing(dataframe)

    # 求单个数值占整个序列数值之和的比重
    proportion_df = get_proportion(normal_df)

    # 熵权法确定指标权重
    weight_ent = get_weight_by_entropy(proportion_df)
    df_weight = df_weight.append(weight_ent)

    # 均方差决策法确定指标权重
    weight_msd = get_weight_by_msd(proportion_df)
    df_weight = df_weight.append(weight_msd)

    # 求综合权重
    weights = 0.5 * (weight_ent + weight_msd)
    weights.name = '综合权重'
    print('----------综合权重----------')
    print(weights)
    df_weight = df_weight.append(weights)
    print('----------权重----------')
    print(df_weight)

    # 计算指标得分
    scores_df = pd.DataFrame(columns=normal_df.columns, index=normal_df.index)
    for i in weights.index:
        scores_df[i] = weights[i] * normal_df[i]
    print('----------指标得分----------')
    print(scores_df)
    N = scores_df.shape[1]
    # 计算指标综合得分
    scores_df['综合得分'] = scores_df.sum(axis=1)
    print('----------综合得分----------')
    print(scores_df)
    df_sumscore[cx_name] = scores_df['综合得分']/N
    # 保存为excel
    # 先转置
    df_weight_T = pd.DataFrame(df_weight.values.T, columns=df_weight.index, index=df_weight.columns)
    df_weight_T = df_weight_T.round(2)
    df_weight_T.to_excel(prename + cx_name + '-权重.xlsx')
    scores_df.to_excel(prename + cx_name + '-评价得分.xlsx')
    df_w = df_w.append(df_weight_T)
    # 会折线图
    # title = cx_name + '-评价得分演变趋势'
    # plot_line(scores_df, title)
# df_w.to_excel(excel_name+'-权重.xlsx')
df_sumscore[zb_type] = df_sumscore.sum(axis=1)
df_sumscore.round(5)
df_sumscore.to_excel(prename + '综合得分.xlsx')
title = prename + '综合水平演变趋势'
print(title)
plot_line(df_sumscore, title)
